<?php
/**
 * 前台专用控制器
、 * 设置默认的视图目录,设置默认视图扩展名为php
 * @package front
 */
class FrontAction extends Request
{
    
	/**
	 * 系统配置信息
	 * 
	 * @var Array
	 */
    public $cfg;
    
    /**
     * 数据库对象
     *
     * @var Mysql
     */
    public $db;
    
    /**
     * 重载控制器
     * 定义默认的视图路径，文件编码，视图扩展名
     * 并调用权限认证
     */
    public function __construct (Route $route) {
        parent::__construct($route);
        $this->setViewPath(dirname ($route->getControllerPath ()) . '/views')//设置视图路径
             ->setAutoRender (false)//设置是否自动渲染页面
             ->setSuffix ('.php');//设置视图后缀名
        
        $controllerName         = $this->getParam('controller');
        $actionName             = $this->getParam('action');
        
        $this->view->baseUrl    = $this->getBaseUrl ();
        $this->view->fullUrl    = $this->getHost() . $this->getBaseUrl ();
        $this->view->controllerName = $controllerName;
        
        $this->checkLogin($controllerName, $actionName);
        
        global $cfg;
        $this->cfg = $cfg;
        $this->view->cfg = $cfg;
        $this->db = Mysql::getInstance();
    }
    
    /**
     * 登录检测
     *
     * @param unknown_type $controllerName
     * @param unknown_type $actionName
     */
    private function checkLogin($controllerName, $actionName)
    {
    	if ($controllerName == 'index'){
    		if (in_array($actionName, array("index", "flash", "info", "dologin", "logout", "doreg"))){
    			return ;
    		}
    	}
    	
    	if (empty($_SESSION['uid'])){
    		$this->showmessage(0, "你还未登录", 1);
    	}
    }
    
    
    /**
     * 输出错误信息
     */
    public function showmessage ($status, $message='', $type='') {
        $message = array("status" => $status, "message" => $message);
        if($type != "")
        	$message['type'] = $type;
    	
    	echo json_encode($message);
    	exit;
    }
    
    /**
     * 内部跳转
     *
     * @param unknown_type $controller
     * @param unknown_type $action
     * @param unknown_type $param
     */
    protected function redirectAction($controller, $action, $param = '') {
    	$url = $this->url($this->view->baseUrl, $controller, $action, $param);
    	header("location:" . $url);
    	exit;
    }
}